/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.Temporal;

/**
 *
 * @author Administrador
 */
@Entity
@NamedQuery(name = "HistorialComite.findByNombre", query = "SELECT c FROM HistorialComite c WHERE c.representante = :representante")
public class HistorialComite implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ManyToOne
    private Representante representante;
    @ManyToOne
    private ComitePrograma comiteprograma;
    String rol;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date fechaIng;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date fechaSal;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRol() {
        return rol;
    }

    public void setRol(String rol) {
        this.rol = rol;
    }

    public ComitePrograma getComiteprograma() {
        return comiteprograma;
    }

    public void setComiteprograma(ComitePrograma comiteprograma) {
        this.comiteprograma = comiteprograma;
    }

    public Date getFechaIng() {
        return fechaIng;
    }

    public void setFechaIng(Date fechaIng) {
        this.fechaIng = fechaIng;
    }

    public Date getFechaSal() {
        return fechaSal;
    }

    public void setFechaSal(Date fechaSal) {
        this.fechaSal = fechaSal;
    }

    public Representante getRepresentante() {
        return representante;
    }

    public void setRepresentante(Representante representante) {
        this.representante = representante;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof HistorialComite)) {
            return false;
        }
        HistorialComite other = (HistorialComite) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Modelo.HistorialComite[id=" + id + "]";
    }

    public void agregarHistorial(HistorialComite historial) {



        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        em.persist(historial);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

    }

    public void actualizarHistorial(HistorialComite historial) {



        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        em.merge(historial);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

    }

    public HistorialComite findAllPetsByName(Representante representante) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Query q = em.createNamedQuery("HistorialComite.findByNombre");
        q.setParameter("representante", representante);

        return (HistorialComite) q.getSingleResult();

    }
}
